package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.TableEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.dto.notice.NoticeInsertUpdateDto;
import com.hbx.model.Notice;
import com.hbx.mapper.NoticeMapper;
import com.hbx.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.utils.Result;
import com.hbx.vo.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-01-22
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 分页查询所有反馈人
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @MarkLog(operate = OperaTypeEnum.SELECT,tables = TableEnum.NOTICE,desc = "查询所有反馈人信息")
    public Result getNoticeList(Integer pageNum, Integer pageSize) {
        Result r = new Result();
        PageBean pageBean = new PageBean();
        Page<Notice> page = new Page<>(pageNum,pageSize);
        IPage<Notice> noticeIPage = noticeMapper.selectPage(page, null);
        pageBean.setTotal(noticeIPage.getTotal());
        pageBean.setPageSize(noticeIPage.getSize());
        pageBean.setPageNum(noticeIPage.getCurrent());
        pageBean.setData(noticeIPage.getRecords());
        return r.setCode(200).setData(pageBean).setMsg("success");
    }

    /**
     * 添加或修改反馈人
     * @param notice
     * @param r
     */
    @Override
    public void insertOrUpdateNotice(NoticeInsertUpdateDto notice, Result r) {
        Integer result = 0;
        Integer id = notice.getId();
        String noticeEmail = notice.getNoticeEmail();
        Notice noticeEntity = new Notice();
        BeanUtils.copyProperties(notice, noticeEntity);
        if(ObjectUtils.isEmpty(id)){
            // 添加
            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            wrapper.eq("notice_email",noticeEmail);
            List<Notice> notices = noticeMapper.selectList(wrapper);
            if (!CollectionUtils.isEmpty(notices)){
                r.setCode(ResponseStateConstant.OPERA_FAIL).setMsg("操作失败,该邮箱已经存在");
                return;
            }
            result = noticeMapper.insert(noticeEntity);
        } else {
            result = noticeMapper.updateById(noticeEntity);
        }

        if (result > 0){
            r.setCode(ResponseStateConstant.OPERA_SUCCESS).setMsg("操作成功");
        }else {
            r.setCode(ResponseStateConstant.OPERA_FAIL).setMsg("操作失败");
        }
    }
}
